﻿var section = {
	init : function( ){
		$.getScriptSafe('/static/settings.js', section.loadMlPack);
	},
	
	loadMlPack : function(){
		$.getScriptSafe('/static/js/lib/ml-utils.js', section.allLoadsComplete);
	},
	
	allLoadsComplete: function () {
		$('.shop-name').append($('<span/>').addClass('you'));
		$('.shop-name ul li.name input').bind('keyup', section.magazineYou);
		$('.shop-name ul li.name input').bind('keyup', section.checkName);
		$('.shop-name ul li.name input').attr('maxlength', '20');
		
		$('.shop-name').append($('<span/>').addClass('you-desc'));
		$('.shop-name ul li.descriptive input').bind('keyup', section.magazineYouDesc);
		
		$('.pagination a.fR').bind('click', section.postForm);
		
		$('.specialty ul li')
			.bind('mouseenter', function (e) {
				$(this).addClass('hover');
			})
			.bind('mouseleave', function (e) {
				$(this).removeClass('hover');
			})
			.bind('click', function (e) {
				$('.specialty ul li').removeClass('current');
				$(this).addClass('current');
				
				$('.specialty ul li').attr('checked', false);
				$(this).find('input').attr('checked', 'checked');
			});
	},

   checkNameTimeout: null,
	checkName: function(e){
		var foo = $(e.currentTarget).val().replace(/[^a-z0-9._]/g,'');
		$(e.currentTarget).val(foo);
		
		clearTimeout(section.checkNameTimeout)
		section.checkNameTimeout = setTimeout(section.checkNameAction, 2000, e);
 	},

	checkNameAction: function(e){
		var text = $(e.currentTarget).val().replace(/[^a-z0-9._]/g,'');
			
		$.ajax({
			type: 'post',
			dataType: 'json',
			contentType: 'application/json',
			processData: false,
			data: new mlSerializer().add('name', text).render(),
			url: settings.showcase.nameFetch,
			success: function(data, xhr, stt){
				var success = data.success;
				var bgPos = success ? 'top left' : 'bottom left';
				$('.about-shop .shop-name ul li .alert')
					.css('background-position', bgPos)
					.show();
				$('input[name="valid_name"]').val(success);	
			}
		});
 	},
	
	postForm: function(e){
		e.preventDefault();

		var isValid = true;
		//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
		if($('input[name="username"]').val() == '' || $('input[name="valid_name"]').val() == 'false'){
			$('input[name="username"]').parent().parent().find('.input').addClass('error');
			isValid = false;
		}else{
			$('input[name="username"]').parent().parent().find('.input').removeClass('error');
		}
		//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
		if($('input[name="description"]').val() == ''){
			$('input[name="description"]').parent().parent().find('.input').addClass('error');
			isValid = false;
		}else{
			$('input[name="description"]').parent().parent().find('.input').removeClass('error');
		}
		//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

		if(isValid){
			trace('Ok')
			$('form').submit();
		}else{
			if($('.input.error').length == 0) return;	
			
			$('html, body').animate({
				scrollTop: $('.input.error:eq(0)').offset().top - 50
			}, {
            duration: 800,
            queue: false
        });
		  return; 
		}
	},
	
   magazineYou: function(){
       var _text = $('.shop-name ul li.name input').val().replace(/[^a-z0-9._]/g,'');
       $('.you').text(_text);
   },
	
   magazineYouDesc: function(){
       var _text = $('.shop-name ul li.descriptive input').val();
       $('.you-desc').text(_text);
   }
};

$(document).ready(section.init);